import json
import random

from masterServer.comm.app import api
from public.config import data_path

with open(data_path('idioms.json'), encoding='utf-8') as f:
    data = json.load(f)  # 原始数据，首字可能和键不一样，用于和小冰对接
    data_true = {}  # 处理后的数据，首字和键一样，用于成语接龙游戏
    for i in data.values():
        for j in i:
            data_true.setdefault(j[0], []).append(j)


@api.get('/idioms-solitaire/get-next')
def get_next(question: str):
    if temp := [v for v in data_true.get(question, []) if v[-1] in data_true]:  # 排除掉无法再接的难题
        text = random.choice(temp)
        return text
